<?php 
class bot_admin 
{ 
	function admin_bot_list()
	{ 
		global $mysql_conn,$v_file;
		$bot_user_id = $_COOKIE['user_id'];
		$list_page = $_GET['list_page'];
		$list_num = $_GET['list_num'];
		$sousuokey = $_GET['sousuokey'];
		if (empty($list_page)) $list_page = 1;
		if (empty($list_num)) $list_num = 10;
		if ($list_page < 5) {$list_page0 = 1;} else {$list_page0 = $list_page-4;}
		if ($list_page < 5) {$list_page1 = 1;} else {$list_page1 = $list_page-3;}
		if ($list_page < 5) {$list_page2 = 2;} else {$list_page2 = $list_page-2;}
		if ($list_page < 5) {$list_page3 = 3;} else {$list_page3 = $list_page-1;}
		if ($list_page < 5) {$list_page4 = 4;} else {$list_page4 = $list_page;}
		if ($list_page < 5) {$list_page5 = 5;} else {$list_page5 = $list_page+1;}
		if ($list_page < 5) {$list_page6 = 6;} else {$list_page6 = $list_page+2;}
		if ($list_page < 5) {$list_page7 = 7;} else {$list_page7 = $list_page+3;}
		if ($list_page < 5) {$list_page8 = 8;} else {$list_page8 = $list_page+4;}
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page0.")'><i class='fa fa-chevron-left'></i></button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page1."' onClick='list_page_go(".$list_page1.")'>".$list_page1."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page2."' onClick='list_page_go(".$list_page2.")'>".$list_page2."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page3."' onClick='list_page_go(".$list_page3.")'>".$list_page3."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page4."' onClick='list_page_go(".$list_page4.")'>".$list_page4."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page5."' onClick='list_page_go(".$list_page5.")'>".$list_page5."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page6."' onClick='list_page_go(".$list_page6.")'>".$list_page6."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page7."' onClick='list_page_go(".$list_page7.")'>".$list_page7."</button>";
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page8.")'><i class='fa fa-chevron-right'></i></button>";
		$list_limit = ($list_page-1)*$list_num.",".$list_num;
		if (empty($sousuokey)) {
			$sql = "SELECT * from `bot` limit ".$list_limit;
		} else {
			$sql = "SELECT * from `bot` WHERE `bot_name` = '".$sousuokey."'";
		}
		$result = mysql_query($sql,$mysql_conn);
		$bot_state_type = array (
			"init"  => "初始状态",
			"loading" => "加载插件",
			"scaning"   => "等待手机扫码",
			"confirming" => "等待手机确认登录",
			"updating" => "更新账户信息",
			"running" => "运行中",
			"stop" => "停止运行",
			"client already exists" => "命令成功执行",
			"failure" => "没有找到客户端信息"
		);
		while(@$row=mysql_fetch_array($result)) {
			$bot_id = $row['bot_id'];
			$bot_name = $row['bot_name'];
			$bot_server_id = $row['bot_server_id'];
			$bot_user_id = $row['bot_user_id'];
			$sql_server = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."'";
			$result_server = mysql_query($sql_server,$mysql_conn);
			@$row_server=mysql_fetch_array($result_server);
			$bot_server_name = $row_server['server_name'];
			$server_url= $row_server['server_url'];
			
			$sql_user = "SELECT * from `user` WHERE `user_id` = '".$bot_user_id."'";
			$result_user = mysql_query($sql_user,$mysql_conn);
			@$row_user=mysql_fetch_array($result_user);
			$bot_user_name = $row_user['user_name'];

			$url = $server_url."openwx/check_client?client=".$bot_id; 
			@$html = file_get_contents($url);
			$contact = json_decode($html);
			$contact_code = "1"//$contact->{'code'};
			if ($contact_code == "0") {
				$contact_client = $contact->{'client'}[0];
				$contact_client_state = $contact_client->{'state'};
				$bot_state = "状态：".$bot_state_type[$contact_client_state];
				$bot_runorstop = "<a href='#' class='btn btn-white btn-xs' onClick='home_bot_stop(".$bot_id.");'><i class='fa fa-stop'></i> 关闭 </a>";
				if ($contact_client_state == "running") {
					$bot_order = "<td><a href='#' class='btn btn-white btn-xs' onClick='home_bot_order(".$bot_id.");'><i class='fa fa-reorder'></i> 管理 </a></td>";
				} else {
					$bot_order = "<td><a href='#' class='btn btn-white btn-xs' onClick=''><i class='fa fa-reorder'></i> 禁用 </a></td>";
				}
			} else if ($contact_code == "1"){
				$bot_state = "机器人已停止";
				$bot_runorstop = "<a href='#' class='btn btn-white btn-xs' onClick='home_bot_run(".$bot_id.");'><i class='fa fa-play'></i> 启动 </a>";
				$bot_order = "<td><a href='#' class='btn btn-white btn-xs' onClick=''><i class='fa fa-reorder'></i> 禁用 </a></td>";
			} else {
				$bot_state = "未知状态";
				$bot_runorstop = "<a href='#' class='btn btn-white btn-xs' onClick=''><i class='fa fa-eject'></i> 禁用 </a>";
				$bot_order = "<td><a href='#' class='btn btn-white btn-xs' onClick=''><i class='fa fa-reorder'></i> 禁用 </a></td>";
			}
			$botlist .= "<tr><td>";
			$botlist .= "    <input type='checkbox' class='i-checks' name='input[]'>";
			$botlist .= "</td>";
			$botlist .= "<td>".$bot_id."</td>";
			$botlist .= "<td>".$bot_name."</td>";
			$botlist .= "<td>".$bot_server_name."</td>";
			$botlist .= "<td>".$bot_user_name."</td>";
			$botlist .= "<td>".$bot_state."</td>";
			$botlist .= "<td>";
			$botlist .= $bot_runorstop;
			$botlist .= "<a href='#' class='btn btn-white btn-xs' onClick='home_bot_edit(".$bot_id.");'><i class='fa fa-edit'></i> 编辑 </a>";
			$botlist .= "<a href='#' class='btn btn-white btn-xs' onClick='home_bot_del(".$bot_id.");'><i class='fa fa-trash-o'></i> 删除 </a>";
			$botlist .= "</td>";
			$botlist .= $bot_order;
			$botlist .= "</tr>";
		}
		require($v_file);
	}
	function admin_bot_add() 
	{ 
		global $mysql_conn,$v_file;
		$sql = "SELECT * from `server` ";
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$server_id = $row['server_id'];
			$server_name = $row['server_name'];
			$serverlist .= "<option value='".$server_id."' >".$server_name."</option>";
		}
		require($v_file);
	}
	function home_bot_add_do() 
	{ 
		global $mysql_conn;
		$user_id = $_COOKIE['user_id'];
		$bot_name = $_POST['bot_name'];
		$bot_server_id = $_POST['bot_server_id'];
		$bot_user_id = $_COOKIE['user_id'];
		$sql = "SELECT * from `user` WHERE `user_id` = '".$user_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$user_bot_num = $row['user_bot_num'];
		$sql = "SELECT * from `bot` WHERE `bot_user_id` = '".$bot_user_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		if ($user_bot_num > mysql_num_rows($result) ) {
			$sql="INSERT INTO bot (bot_name, bot_server_id, bot_user_id) VALUES ('$bot_name','$bot_server_id','$bot_user_id')";
			$result = mysql_query($sql,$mysql_conn);
			@$row=mysql_fetch_array($result);
			echo "2";//创建成功
		} else {
			echo "1";//超出可用机器人数量
		}
	}
	function home_bot_edit() 
	{ 
		global $mysql_conn,$v_file;
		$bot_user_id = $_COOKIE['user_id'];
		$bot_id = $_GET['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_user_id` = '".$bot_user_id."' and `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_name = $row['bot_name'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` ";
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$server_id = $row['server_id'];
			$server_name = $row['server_name'];
			$server_url = $row['user_id'];
			if ($server_id == $bot_server_id) {
				$serverlist .= "<option value='".$server_id."' selected>".$server_name."</option>";
			} else {
				$serverlist .= "<option value='".$server_id."' >".$server_name."</option>";
			}
		}
		require($v_file);
	}
	function home_bot_edit_do() 
	{ 
		global $mysql_conn;
		$bot_id = $_POST['bot_id'];
		$bot_name = $_POST['bot_name'];
		$bot_server_id = $_POST['bot_server_id'];
		$bot_user_id = $_COOKIE['user_id'];
		$sql = "UPDATE `bot` SET `bot_name` = '".$bot_name."',`bot_server_id` = '".$bot_server_id."' WHERE `bot_id` =".$bot_id." and `bot_user_id` =".$bot_user_id." ;";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		echo "2";//创建成功
	}
	function home_bot_del_do() 
	{ 
		global $mysql_conn;
		$bot_user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "DELETE from `bot` WHERE `bot_id` = '".$bot_id."' and `bot_user_id` = '".$bot_user_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
	}
} 
/* End of file democontroller.php */ 